package com.weilai.service.serviceImpl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.weilai.controller.exceptionHandler.BusinessException;
import com.weilai.controller.exceptionHandler.Code;
import com.weilai.dao.AdminDao;
import com.weilai.dao.PostsDao;
import com.weilai.dao.UserDao;
import com.weilai.domain.*;
import com.weilai.service.AdminService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

@Service
public class AdminServiceImpl implements AdminService {

    private static final Logger logger = LoggerFactory.getLogger(AdminServiceImpl.class);

    @Autowired
    private AdminDao adminDao;
    @Autowired
    private UserDao userDao;
    @Autowired
    private PostsDao postsDao;
    @Autowired
    private ReportInfoServiceImpl reportInfoServiceImpl;

    @Override
    public List<Comments> commentsUnderChecking() {
        List<Comments> comments = adminDao.commentsUnderChecking();
        return comments;
    }

    @Override
    public Categories selectCategoryById(int id) {
        Categories category = adminDao.selectCategoryById(id);
        if (category == null) {
            logger.warn("未找到 ID 为 {} 的类别", id);
            throw new BusinessException(Code.PROJECT_DATA_NOT_FOUND, "未找到指定 ID 的类别");
        }
        return category;
    }

    @Override
    public List<Categories> selectAllCategory() {
        List<Categories> categories = adminDao.selectAllCategory();
        if (categories.isEmpty()) {
            logger.warn("类别数据为空");
            throw new BusinessException(Code.PROJECT_DATA_NOT_FOUND, "没有类别数据");
        }
        return categories;
    }

    @Override
    public void manageCategory(Categories category) {
        if (category == null) {
            logger.error("类别对象为空");
            throw new BusinessException(Code.PROJECT_VALIDATE_ERROR, "类别对象不能为空");
        }
        adminDao.manageCategory(category);
        logger.info("成功管理类别: {}", category);
    }

    @Override
    public void insertCategory(Categories category) {
        if (category == null) {
            logger.error("类别对象为空");
            throw new BusinessException(Code.PROJECT_VALIDATE_ERROR, "类别对象不能为空");
        }

        // 类别名称是否已存在??
        adminDao.insertCategory(category);
        logger.info("成功插入类别: {}", category);
    }

    @Override
    public void deleteCategoryById(int id) {
        Categories category = selectCategoryById(id);
        if (category == null) {
            logger.warn("未找到 ID 为 {} 的类别，无法删除", id);
            throw new BusinessException(Code.PROJECT_DATA_NOT_FOUND, "未找到指定 ID 的类别，无法删除");
        }
        adminDao.deleteCategoryById(id);
        logger.info("成功删除 ID 为 {} 的类别", id);
    }

    @Override
    public List<Posts> selectPostsUnderChecking() {
        List<Posts> posts = adminDao.selectPostsUnderChecking();
        if (posts.isEmpty()) {
            logger.warn("没有未审核的帖子");
        }
        return posts;
    }

    @Override
    public void approvePost(int id) {
        if (postsDao.selectById(id) == null) {
            logger.warn("未找到 ID 为 {} 的帖子，无法审核", id);
            throw new BusinessException(Code.PROJECT_DATA_NOT_FOUND, "未找到指定 ID 的帖子，无法审核");
        }
        adminDao.approvePost(id);
        logger.info("成功审核 ID 为 {} 的帖子", id);
    }

    @Override
    public void muteUser(int id) {
        if (userDao.getById(id) == null) {
            logger.warn("未找到 ID 为 {} 的用户，无法禁言", id);
            throw new BusinessException(Code.PROJECT_DATA_NOT_FOUND, "未找到指定 ID 的用户，无法禁言");
        }
        adminDao.muteUser(id);
        logger.info("成功禁言 ID 为 {} 的用户", id);
    }

    @Override
    public void unMuteUser(int id) {
        if (userDao.getById(id) == null) {
            logger.warn("未找到 ID 为 {} 的用户，无法解除禁言", id);
            throw new BusinessException(Code.PROJECT_DATA_NOT_FOUND, "未找到指定 ID 的用户，无法解除禁言");
        }
        adminDao.unMuteUser(id);
        logger.info("成功解除 ID 为 {} 的用户禁言", id);
    }

    @Override
    public void rejectComment(int id) {
        try {
            if (adminDao.selectCommentById(id) == null) {
                logger.warn("未找到 ID 为 {} 的评论，无法拒绝", id);
                throw new BusinessException(Code.PROJECT_DATA_NOT_FOUND, "未找到指定 ID 的评论，无法拒绝");
            }
            adminDao.rejectComment(id);
            logger.info("成功拒绝 ID 为 {} 的评论", id);
        } catch (BusinessException e) {
            throw e;
        } catch (Exception e) {
            logger.error("拒绝评论失败，ID: {}", id, e);
            throw new BusinessException(Code.PROJECT_DATA_NOT_FOUND, "评论拒绝失败");
        }
    }

    @Override
    public void approveComment(int id) {
        try {
            if (adminDao.selectCommentById(id) == null) {
                logger.warn("未找到 ID 为 {} 的评论，无法通过审核", id);
                throw new BusinessException(Code.PROJECT_DATA_NOT_FOUND, "未找到指定 ID 的评论，无法通过审核");
            }
            adminDao.approveComment(id);
            logger.info("成功通过 ID 为 {} 的评论审核", id);
        } catch (BusinessException e) {
            throw e;
        } catch (Exception e) {
            logger.error("通过评论审核失败，ID: {}", id, e);
            throw new BusinessException(Code.PROJECT_DATA_NOT_FOUND, "评论审核失败");
        }
    }

    @Override
    public Comments selectCommentById(int id) {
        Comments comment = adminDao.selectCommentById(id);
        if (comment == null) {
            logger.warn("未找到 ID 为 {} 的评论", id);
            throw new BusinessException(Code.PROJECT_DATA_NOT_FOUND, "未找到指定 ID 的评论");
        }
        return comment;
    }

    @Override
    public User fuzzyQueryUser(String keyWord) {
        if(keyWord==null){
            throw new BusinessException(Code.PROJECT_BUSINESS_ERROR,"不可查询空值");
        }
        return adminDao.fuzzyQueryUser(keyWord);
    }

    //后台分页
    @Override
    public PageInfo<User> getUsersFromManagerWithPaging(int pageNum, int pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        return new PageInfo<>(userDao.getAllUsers());
    }

    @Override
    public PageInfo<Posts> getPostsFromManagerWithPaging(int pageNum, int pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        return new PageInfo<>(adminDao.selectPostsUnderChecking());
    }

    @Override
    public PageInfo<Categories> getCategoriesFromManagerWithPaging(int pageNum, int pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        return new PageInfo<>(adminDao.selectAllCategory());
    }

    @Override
    public PageInfo<ReportInfo> getReportInfoFromManagerWithPaging(int pageNum, int pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        return new PageInfo<>(reportInfoServiceImpl.selectReportedInfo());
    }
}